<%@ include file="/WEB-INF/pages/common/taglibs.jsp" %>
<%@ include file="/WEB-INF/pages/common/checkpermission.jsp" %>

<script type="text/javascript">
    var subWin=null;
    <s:if test='#attr.user.userRole=="O"'>
    function addPdfDetail(pageNum, addition) {
        if (addition) pageNum = parseInt(pageNum) + addition;
        var currentForm = document.forms["editForm"];
        openNewWindow("about:blank", 150, 500, null, 1, null, null, null, null, "popWindow");
        currentForm.elements["erecordModel.selectPageNum"].value = pageNum;
        currentForm.target = "popWindow";
        currentForm.action="viewAddPdfDetail.action";
        currentForm.submit();
    }
                                                 
    function deletePdfDetail(pageNum){
        var currentForm = document.forms["editForm"];
        if(currentForm.elements["total"].value==1){
            alert("<s:text name='label.last.page.cannot.delete'/>");
            return;
        }
        currentForm.elements["erecordModel.selectPageNum"].value = pageNum;
        subWin=openNewWindow("about:blank", 150, 500, null, 1, null, null, null, null, "popWindow");
        currentForm.target = "popWindow";
        currentForm.action="preDeletePdfDetail.action";
        currentForm.submit();
    }

    function submitDeletePdfDetail(){
        if (subWin != null && subWin.open) subWin.close();
        if (window.confirm('<s:text name="label.confirm.delete"/>')) {
            var currentForm = document.forms["editForm"];
            currentForm.target = "_self";
            currentForm.action="deletePdfDetail.action";
            currentForm.submit();
        }
    }

    function saveRecord() {
        var currentForm = document.forms["editForm"];
        currentForm.elements["erecordModel.recordName"].value = currentForm.elements["erecordModel.recordName"].value.trim();
        currentForm.elements["erecordModel.year"].value = currentForm.elements["erecordModel.year"].value.trim();
        currentForm.elements["erecordModel.refNo"].value = currentForm.elements["erecordModel.refNo"].value.trim();
        currentForm.elements["erecordModel.source"].value = currentForm.elements["erecordModel.source"].value.trim();
        currentForm.elements["erecordModel.appellee"].value = currentForm.elements["erecordModel.appellee"].value.trim();
        currentForm.elements["erecordModel.undertaker"].value = currentForm.elements["erecordModel.undertaker"].value.trim();
        currentForm.elements["erecordModel.caseReason"].value = currentForm.elements["erecordModel.caseReason"].value.trim();
        currentForm.elements["erecordModel.remark"].value = currentForm.elements["erecordModel.remark"].value.trim();
        if(isEmpty(currentForm.elements["erecordModel.recordName"].value)){
            alert('<s:text name="label.erecord.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.year"].value)){
            alert('<s:text name="label.year.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.refNo"].value)){
            alert('<s:text name="label.refno.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.source"].value)){
            alert('<s:text name="label.source.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.appellee"].value)){
            alert('<s:text name="label.appellee.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.caseDate"].value)){
            alert('<s:text name="label.casedate.required"/>');
            return;
        }
        if(isEmpty(currentForm.elements["erecordModel.caseReason"].value)){
            alert('<s:text name="label.casereason.required"/>');
            return;
        }
        if(!validateForm_int(currentForm.elements["erecordModel.year"])){
            alert('<s:text name="label.year.error"/>');
            return;
        }
        if(parseInt(currentForm.elements["erecordModel.year"].value)<1900 ||parseInt(currentForm.elements["erecordModel.year"].value)>2050){
            alert('<s:text name="label.year.error"/>');
            return;
        }

        if(!validateForm_date(currentForm.elements["erecordModel.caseDate"])){
            alert('<s:text name="label.casedate.error"/>');
            return;
        }
        submitForm("editForm", "save.action");
    }
    </s:if>    


    function viewPdfDetail(pageNum) {
        var currentForm = document.forms["editForm"];
        var   w=screen.availWidth;
        var   h=screen.availHeight;
        openNewWindow("about:blank", h, w, null, 1, null, null, null, null, "popWindow");
        currentForm.elements["erecordModel.selectPageNum"].value = pageNum;
        currentForm.target = "popWindow";
        currentForm.action= "viewPdfDetail.action";
        currentForm.submit();
    }


    function cancelRecord() {
        if(window.confirm('<s:text name="label.confirm.back"/>')){   
        var currentForm = document.forms["editForm"];
        submitForm("editForm", "search.action");
        }
    }

    function refresh(){
        var currentForm = document.forms["editForm"];
        currentForm.target="_self";
        currentForm.action="searchDetail.action";
        currentForm.submit();
    }

    function checkPrint(obj){
        var currentForm = document.forms["editForm"];
        if(obj.checked){
            if(currentForm.elements["erecordModel.printArray"].value==""){
               currentForm.elements["erecordModel.printArray"].value=obj.value;
            }else if(!(checkStart(currentForm.elements["erecordModel.printArray"].value,obj.value+";")||
                     currentForm.elements["erecordModel.printArray"].value.indexOf(";"+obj.value+";")!=-1||
                     checkEnd(currentForm.elements["erecordModel.printArray"].value,";"+obj.value))){
                currentForm.elements["erecordModel.printArray"].value=currentForm.elements["erecordModel.printArray"].value+";"+obj.value;
            }
        }else{
            if(currentForm.elements["erecordModel.printArray"].value==obj.value){
               currentForm.elements["erecordModel.printArray"].value="";
            }else if(checkStart(currentForm.elements["erecordModel.printArray"].value,obj.value+";")){
                currentForm.elements["erecordModel.printArray"].value=
                currentForm.elements["erecordModel.printArray"].value.substring(obj.value.length+1);
            }else if(checkEnd(currentForm.elements["erecordModel.printArray"].value,";"+obj.value)){
                currentForm.elements["erecordModel.printArray"].value=
                currentForm.elements["erecordModel.printArray"].value.substring(0,currentForm.elements["erecordModel.printArray"].value.length-obj.value.length-1);
            }else{
                var index=currentForm.elements["erecordModel.printArray"].value.indexOf(";"+obj.value+";");
                if(index!=-1){
                    currentForm.elements["erecordModel.printArray"].value=
                    currentForm.elements["erecordModel.printArray"].value.substring(0,index)+
                    currentForm.elements["erecordModel.printArray"].value.substring(index+1+obj.value.length);
                }
            }
        }
    }

    function printAll(){
        if(window.confirm('<s:text name="label.print.all"/>')){
            var currentForm = document.forms["editForm"];
            currentForm.target="embedFrame";
            currentForm.action="printAll.action";
            currentForm.submit();
         }
    }

    function partialPrint(){
        if(document.getElementById("erecordModel.printArray").value==""){
            alert('<s:text name="label.select.atleast.one.record"/>');                
        }else{
            var arr=document.getElementById("erecordModel.printArray").value.split(";");
            if(window.confirm('<s:text name="label.gong"/>'+arr.length+'<s:text name="label.confirm.print"/>')){
            var currentForm = document.forms["editForm"];
            currentForm.target="_self";
            currentForm.action="printSelect.action";
            currentForm.submit();
            }

        }

    }

    function checkprintPages(){
        var arr=document.getElementById("erecordModel.printArray").value.split(";");
        var o;
        var resultList;
        if (isEmpty("editFormDiv")) {
            resultList = document.getElementsByTagName("input");
        } else {
            o = document.getElementById("editFormDiv");
            resultList = o.getElementsByTagName("input");
        }
        for (var i = 0; i < resultList.length; i++) {
            if (resultList[i].type == 'checkbox') {
                resultList[i].checked="";
                for (var j = 0; j < arr.length; j++) {
                    if (arr[j] == resultList[i].value) {
                        resultList[i].checked="checked";
                    }
                }
            }
        }
    }

    function downloadAttach(){
        submitForm("editForm","download.action");
    }

</script>

<div id='editFormDiv'>
    <s:form name="editForm" action="" method="post">
        <s:hidden name="erecordModel.recordId"/>
        <s:hidden name="erecordModel.selectId"/>
        <s:hidden name="erecordModel.selectPageNum"/>
        <s:hidden name="erecordModel.printArray"/>        
        <s:hidden name="erecordModel.createId"/>
        <s:hidden name="erecordModel.createDt"/>
        <s:hidden name="total"/>
        <s:hidden id="erecordModel.deletePassword" name="erecordModel.deletePassword" value="" />
        <table align="center" cellpadding="0" cellspacing="0" class="body_content full_width">
            <tr>
                <td align="left" valign="bottom" class="main_page_content_icon" width="4%">
                    <img src='<c:url value="/images/body-icon_check.gif"/>' width="29" height="44">
                </td>
                <td valign="bottom" class="page_title_bg" width="96%">
                <span class="page_title">
                    <s:text name="label.update.erecord"/>
                </span>
                </td>
            </tr>
            <tr>
                <td class="subcontent" colspan="2">&nbsp;</td>
            </tr>
        </table>

        <table border="0" align="center" cellpadding="0" cellspacing="0" class="body_content full_width bordered">
            <tr class="even">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.name.erecord" /></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%"><s:textfield name="erecordModel.recordName" maxlength="100" /></td>
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.type.erecord" /></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%">
                <s:select list="typeList" name="erecordModel.recordType" listKey="value" listValue="label"/>
            </td>
		</tr>
		<tr class="odd">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.year"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%"><s:textfield name="erecordModel.year" maxlength="4"/></td>
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.ref.no" /></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%">
                <s:textfield name="erecordModel.refNo" maxlength="25"/>
            </td>
		</tr>
		<tr class="even">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.source"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%"><s:textfield name="erecordModel.source" maxlength="100" /></td>
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.appellee"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%">
                <s:textfield name="erecordModel.appellee" maxlength="25"/>
            </td>
		</tr>
		<tr class="odd">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.casedate"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%"><cl:date name="erecordModel.caseDate"/>(<s:text name='label.date.format'/>)</td>
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.undertaker"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%">
                <s:textfield name="erecordModel.undertaker" maxlength="25" />
            </td>
		</tr>
		<tr class="even">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.caseresaon"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%" colspan="4"><s:textfield name="erecordModel.caseReason" maxlength="500" size="150"/></td>
		</tr>
		<tr class="odd">
			<td class="form_label_padded" width="10%" nowrap><s:text name="label.remark"/></td>
			<td class="form_label_padded" width="2%">:</td>
			<td class="txtData" align="left" width="38%" colspan="4"><s:textfield name="erecordModel.remark" maxlength="500" size="150"/></td>
		</tr>
		<tr class="odd">
            <td class="txtData" colspan="6">
                <s:if test='#attr.user.userRole=="O"'>
                <input type="button" class="button" value="<s:text name='label.save'/>" onclick="saveRecord();">
                </s:if>
                <input type="button" class="button" value="<s:text name='label.back'/>" onclick="cancelRecord();">
                <input type="button" class="button" value="<s:text name='label.printall'/>" onclick="printAll();">
                <input type="button" class="button" value="<s:text name='label.printselect'/>" onclick="partialPrint();">
                <input type="button" class="button" value="<s:text name='label.viewall'/>" onclick="downloadAttach();">
            </td>
        </tr>
        </table>
        <display:table name="detailHelper" form="editForm" uid="row" action="searchDetail.action">
            <display:column titleKey="label.page" width="25%">
                 &nbsp;
                <div align="center" style="vertical-align:middle;">
                    <s:text name="label.di"/><s:property value="%{#attr.row.pageNo}"/><s:text name="label.ye"/>
                </div>
            </display:column>

            <display:column titleKey="label.print"  width="25%">
                 &nbsp;
                <div align="center" style="vertical-align:middle;">
                    <s:checkbox name="detailResultList[%{#attr.row_rowNum-1}].selectId" fieldValue="%{#attr.row.pageNo}" onclick="checkPrint(this);"/>
                 </div>
            </display:column>

            <display:column titleKey="label.browse"  width="25%">
                <div align="left">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <s:if test='#attr.user.userRole=="O"'>
                        <a href="javascript:addPdfDetail('<s:property value="%{#attr.row.pageNo}"/>')">
                            <img style="vertical-align:middle;" width="50" height="50" src="<s:url value='/images/ico_add.png'/>">
                        </a>
                        &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
                    </s:if>
                    <a href="javascript:viewPdfDetail('<s:property value="%{#attr.row.pageNo}"/>')">
                        <img style="vertical-align:middle;" width="50" height="50" src="<s:url value='/images/pdf_icon.png'/>">
                    </a>
                    <s:if test='#attr.user.userRole=="O" && #attr.row.pageNo==total'>
                         &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;
                        <a href="javascript:addPdfDetail('<s:property value="%{#attr.row.pageNo}"/>')">
                            <img style="vertical-align:middle;" width="50" height="50" src="<s:url value='/images/ico_add.png'/>">
                        </a>
                    </s:if>
                </div>
            </display:column>
            <s:if test='#attr.user.userRole=="O"'>
            <display:column titleKey="label.delete" width="25%">
                <div align="center">
                    &nbsp;
                        <a href="javascript:deletePdfDetail('<s:property value="%{#attr.row.pageNo}"/>')">
                        <img width="50" height="50" style="vertical-align:middle;" src="<s:url value='/images/ico_delete.png'/>">
                        </a>
                </div>
            </display:column>
            </s:if>
        </display:table>
    </s:form>
</div>
<script type="text/javascript">
    checkprintPages();
</script>
<iframe id="embedFrame" name="embedFrame" type="application/pdf" width="100%" height="0"></iframe>
